package com.oujiong.service.impl;

import com.oujiong.entity.User;
import com.oujiong.mapper.UserMapper;
import com.oujiong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author sunhui
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> list() {
        List<User> users = userMapper.selectAll();
        return users;
    }

    //    @Transactional(rollbackFor = Exception.class)
    @Override
    public String insertForeach(List<User> userList) throws Exception {
        List<User> list1 = new ArrayList<>();
        List<User> list2 = new ArrayList<>();
        for (User user : userList) {
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setStatus(0);
            if (list1.size() < 3) {
                list1.add(user);
            } else {
                list2.add(user);
            }
        }

        //批量插入数据
        userMapper.insertForeach(userList);
        userMapper.insertForeach(userList);
//        throw new Exception("error");
        return "保存成功";
        //批量插入数据

    }
}
